<?php

namespace app\health\model;

use think\Model;

/**
 * 站点轮播图模型
 */
class HealthWebsiteTask extends Model
{
    // 表名
    protected $name = 'health_website_task';

    /**
     * 任务类型描述
     */
    public const CATEGORY_MAP = [
        1 => '每日任务',
        2 => '每月任务',
        3 => '系统任务',
    ];

    /**
     * 奖励类型
     */
    public const AWARD_TYPE_MAP = [
        1 => '余额',
        2 => '积分',
    ];

    /**
     * 任务完成方式
     */
    public const COMPLETE_WAY_MAP = [
        1 => '看视频',
        2 => '完善个人信息',
        3 => '成功使用任一咨询服务',
        4 => '购买一次会员专属优惠月卡',
        5 => '商城成功下单一次',
    ];

    protected $type = [
        'category'     => 'integral',
        'complete_way' => 'integral',
        'status'       => 'integral',
    ];

    /**
     * 获取完成方式规则
     * @return array
     */
    public static function completeWayRule(): array
    {
        $data = [];
        foreach (self::COMPLETE_WAY_MAP as $name => $label) {
            $rule = [
                'complete_num' => 0,//0自定义完成次数,1只能完成一次
            ];
            $type = 0;
            if (in_array($name, [2, 3, 4, 5])) {
                $rule['complete_num'] = 1;
                $type                 = 1;
            }
            $data[] = [
                'name'  => $name,
                'label' => $label,
                'type'  => $type,
                'rule'  => $rule,
            ];
        }
        return $data;
    }

    /**
     * 获取完成方式跳转页面
     * @param $completeWay
     * @return string
     */
    public static function getCompleteToPage($completeWay)
    {
        $page = [
            2 => '/pages/user/userInfo/index',
            3 => '/pages/user/equityCard/index',
            4 => '/pages/user/receive/index',
            5 => '/pages/main/user/index',
        ];
        return $page[$completeWay] ?? '';
    }

    /**
     * 任务类型描述文本
     * @param [type] $val
     * @param [type] $data
     * @return string
     * @Author 张旭鹏
     * @DateTime 2023-07-17
     */
    public function getCategoryTextAttr($val, $data): string
    {
        $category = $data['category'] ?? 0;
        return self::CATEGORY_MAP[$category] ?? '-';
    }

    /**
     * 奖励类型描述文本
     * @param [type] $val
     * @param [type] $data
     * @return string
     * @Author 张旭鹏
     * @DateTime 2023-07-17
     */
    public function getAwardTypeTextAttr($val, $data): string
    {
        $award_type = $data['award_type'] ?? 0;
        return self::AWARD_TYPE_MAP[$award_type] ?? '-';
    }


    /**
     * 任务完成方式描述文本
     * @param [type] $val
     * @param [type] $data
     * @return string
     * @Author 张旭鹏
     * @DateTime 2023-07-17
     */
    public function getCompleteWayTextAttr($val, $data): string
    {
        $complete_way = $data['complete_way'] ?? 0;
        return self::COMPLETE_WAY_MAP[$complete_way] ?? '-';
    }

    /**
     * 状态描述
     * @param [type] $val
     * @param [type] $data
     * @Author 张旭鹏
     * @DateTime 2023-07-13
     */
    public function getStatusTextAttr($val, $data): string
    {
        $map = [1 => '启用中', 0 => '禁用中'];
        return $map[$data['status']];
    }

    public function setCompleteWayContentAttr($v)
    {
        return json_encode($v, JSON_UNESCAPED_UNICODE);
    }

    public function getCompleteWayContentAttr($v)
    {
        return json_decode($v, true);
    }
}
